package com.tencent.tsf.femas.config;


import com.alibaba.fastjson.JSONObject;
import com.tencent.tsf.femas.entity.param.InstanceUpDownParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.Map;

/**
 * @author riemann
 * @date 2019/05/25 2:20
 */
@Service
public class RestTemplateToInterface {

  @Autowired
  private RestTemplate restTemplate;

  /**
   * 以get方式请求第三方http接口 getForEntity
   *
   * @param url
   * @return
   */
  public InstanceUpDownParam doGetWith1(String url) {
    ResponseEntity<InstanceUpDownParam> responseEntity = restTemplate.getForEntity(url, InstanceUpDownParam.class);
    InstanceUpDownParam instanceUpDownParam = responseEntity.getBody();
    return instanceUpDownParam;
  }

  /**
   * 以get方式请求第三方http接口 getForObject
   * 返回值返回的是响应体，省去了我们再去getBody()
   *
   * @param url
   * @return
   */
  public InstanceUpDownParam doGetWith2(String url) {
    InstanceUpDownParam instanceUpDownParam = restTemplate.getForObject(url, InstanceUpDownParam.class);
    return instanceUpDownParam;
  }

  /**
   * 以post方式请求第三方http接口 postForEntity
   *
   * @param url
   * @return
   */
  public String doPostWith1(String url) {
    InstanceUpDownParam instanceUpDownParam = new InstanceUpDownParam();
    ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, instanceUpDownParam, String.class);
    String body = responseEntity.getBody();
    return body;
  }

  /**
   * 以post方式请求第三方http接口 postForEntity
   *
   * @param url
   * @return
   */
  public String doPostWith2(String url) {
    InstanceUpDownParam instanceUpDownParam = new InstanceUpDownParam();
    String body = restTemplate.postForObject(url, instanceUpDownParam, String.class);
    return body;
  }

  /**
   * exchange
   *
   * @return
   */
  public String doExchange(String url,Map<String, Object> map) {
    //url ="http://192.168.1.60:8888/nacos/v1/ns/instance?serviceName=cat&clusterName=DEFAULT&groupName=DEFAULT_GROUP&ip=192.168.1.60&port=12008&ephemeral=true&weight=1&enabled=false&namespaceId=ns-2znema4d";

    //url后请求参数
    url=url.concat("?"+getUrlParamsByMap(map));

    //header参数
    HttpHeaders headers = new HttpHeaders();
//    headers.add("authorization", token);
    headers.setContentType(MediaType.APPLICATION_JSON);
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<String, Object>();//参数放入// 一个map中，restTemplate不能用hashMap
    param.setAll(map);

    //放入body中的json参数
    JSONObject obj = new JSONObject();
    obj.put("serviceName", "cat");
    obj.put("clusterName", "DEFAULT_GROUP");
    obj.put("ip", "192.168.1.60");
    obj.put("port", "12008");
    obj.put("ephemeral", "true");
    obj.put("weight", "1");
    obj.put("enabled", "false");
    obj.put("namespaceId", "2znema4d");

    //组装
    HttpEntity<MultiValueMap<String, Object>> request=new HttpEntity<>(param,headers);
    ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, request, String.class);
    String result = responseEntity.getBody();
    return result;
  }


  public static String getUrlParamsByMap(Map<String, Object> map) {
    if (map == null) {
      return "";
    }
    StringBuffer sb = new StringBuffer();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      sb.append(entry.getKey() + "=" + entry.getValue());
      sb.append("&");
    }
    String s = sb.toString();
    if (s.endsWith("&")) {
      s = s.substring(0, s.length() - 1);
      //s = org.apache.commons.lang.StringUtils.substringBeforeLast(s, "&");
    }
    return s;
  }
}



